//
//  AccountCell.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/26.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

class AccountCell: UITableViewCell {
    
    var data: AccountModel? {
        didSet {
            if let currentData = data {
                amountLabel.text = String(format: "%.2f", currentData.amount)
                noteLabel.text = currentData.note
                let index = currentData.date.index(currentData.date.startIndex, offsetBy: 5)
                yearLabel.text = currentData.date.substring(to: index)
                monthDayLabel.text = currentData.date.substring(from: index)
            }
        }
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
        layoutUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func prepareUI() {
        self.backgroundColor = UIColor.la_bg
        self.selectionStyle = .none
        self.addSubview(content)
        content.addSubview(leftView)
        content.addSubview(rightView)
        leftView.addSubview(yearLabel)
        leftView.addSubview(monthDayLabel)
        rightView.addSubview(lineView)
        rightView.addSubview(amountTitltLabel)
        rightView.addSubview(amountLabel)
        rightView.addSubview(noteTitleLabel)
        rightView.addSubview(noteLabel)
    }
    
    fileprivate func layoutUI() {
        content.snp.makeConstraints { (make) in
            make.top.left.equalTo(5)
            make.bottom.right.equalTo(-5)
        }
        leftView.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(100)
        }
        yearLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-10)
        }
        monthDayLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(yearLabel.snp.bottom).offset(10)
        }
        rightView.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(leftView.snp.right)
        }
        amountTitltLabel.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(10)
        }
        amountLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(lineView.snp.top).offset(-5)
        }
        lineView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.left.equalTo(10)
            make.height.equalTo(0.5)
        }
        
        noteTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(10)
            make.left.equalTo(10)
        }
        
        noteLabel.snp.makeConstraints { (make) in
            make.top.equalTo(noteTitleLabel.snp.bottom).offset(5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }

    }
    
    fileprivate lazy var content: UIView = {
        let object = UIView()
        object.backgroundColor = .white
        object.layer.cornerRadius = 2
        return object
    }()
    
    fileprivate lazy var leftView: UIView = {
        let object = UIView()
        object.backgroundColor = UIColor.la_app
        return object
    }()
    
    fileprivate lazy var yearLabel: UILabel = {
        let object = UILabel()
        object.textColor = .white
        object.font = UIFont.la_Font(ofSize: 15)
        object.textAlignment = .center
        object.text = "2017年"
        return object
    }()
    
    fileprivate lazy var monthDayLabel: UILabel = {
        let object = UILabel()
        object.textColor = .white
        object.font = UIFont.la_Font(ofSize: 15)
        object.textAlignment = .center
        object.text = "05月12日"
        return object
    }()
    
    fileprivate lazy var rightView: UIView = {
        let object = UIView()
        object.backgroundColor = .white
        return object
    }()
    
    
    fileprivate lazy var amountTitltLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 9)
        object.textAlignment = .center
        object.text = "总资产："
        return object
    }()
    
    fileprivate lazy var amountLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 18)
        object.textAlignment = .center
        object.text = "2999999.00"
        return object
    }()
    
    fileprivate lazy var lineView: UIView = {
        let object = UIView()
        object.backgroundColor = UIColor.la_gray
        return object
    }()
    
    fileprivate lazy var noteTitleLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 9)
        object.textAlignment = .center
        object.text = "备注"
        return object
    }()
    
    fileprivate lazy var noteLabel: UILabel = {
        let object = UILabel()
        object.textColor = UIColor.la_gray
        object.font = UIFont.la_Font(ofSize: 12)
        object.textAlignment = .center
        object.text = "今天记录一下，看一下账本，心情不错"
        return object
    }()

}
